package com.yiqi.liebang.feature.mine.view.adapter;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.T;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.widget.dialog.NormalDialog;
import com.yiqi.liebang.common.widget.dialog.OnBtnClickL;
import com.yiqi.liebang.entity.bo.CommentBo;
import com.yiqi.liebang.entity.bo.UserCenterBo;
import com.yiqi.liebang.framework.api.UserApi;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class CommentsAdapter extends BaseQuickAdapter<UserCenterBo.CommentBean, BaseViewHolder> {
    boolean isLookMe;
    private int isComment;
    public CommentsAdapter(boolean isLookMe, int isComment) {
        super(R.layout.item_userinfo_pinglun);
        this.isLookMe = isLookMe;
        this.isComment=isComment;
    }

    @Override
    protected void convert(BaseViewHolder helper, UserCenterBo.CommentBean item) {
        helper.setText(R.id.tv_user_question_name, item.getUserName() + "")
                .setText(R.id.tv_user_question_position, item.getPosition())
                .setText(R.id.tv_comment_zan, item.getNum() + "")
                .setText(R.id.tv_comment_content, item.getComment())
                .setVisible(R.id.iv_user_position, item.getIsOccupation() == 1 ? true : false)
                .setImageResource(R.id.iv_comment_zan, item.getCommentIsRecord() == 1 ? R.drawable.icon_dianzan_sel : R.drawable.btn_dianzan)
                .addOnClickListener(R.id.iv_comment_head)
        ;

        AM.image().bindToCircleObject(item.getUserHead(), helper.getView(R.id.iv_comment_head));
        if (item.getIsBasic() == 1) {
            helper.getView(R.id.iv_user_certification).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.iv_user_certification).setVisibility(View.GONE);
        }

//        if (isLookMe) {
//            helper.getView(R.id.btn_comment_zan).setVisibility(View.GONE);
//        } else {
//            helper.getView(R.id.btn_comment_zan).setVisibility(View.VISIBLE);
//        }
        CommentBo commentBo = new CommentBo();
        commentBo.setCommentId(item.getId());

        helper.getView(R.id.btn_comment_zan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isComment == 1) {
                    AM.api().createApiService(UserApi.class)
                            .zanComment(commentBo)
                            .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                            .subscribe(new Observer<String>() {
                                @Override
                                public void onSubscribe(Disposable d) {

                                }

                                @Override
                                public void onNext(String s) {
                                    helper.setText(R.id.tv_comment_zan, (item.getNum() + 1) + "")
                                            .setImageResource(R.id.iv_comment_zan, R.drawable.icon_dianzan_sel);
                                }

                                @Override
                                public void onError(Throwable e) {
                                    T.showShort(e.getMessage());
                                }

                                @Override
                                public void onComplete() {

                                }
                            });
                } else {
                    final NormalDialog dialog = new NormalDialog(mContext);
                    dialog.content("您暂无权限点赞该用户！")//
                            .btnNum(1)
                            .btnText("取消")//
                            .show();

                    dialog.setOnBtnClickL(new OnBtnClickL() {
                        @Override
                        public void onBtnClick() {
                            dialog.dismiss();
                        }
                    });
                }

            }
        });


    }
}
